package com.yuncheng.spcyApi.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yuncheng.spcyApi.entity.SpcyRsWjmb;
import com.yuncheng.spcyApi.entity.SpcyRsWjmbZjml;
import com.yuncheng.spcyApi.mapper.SpcyRsWjmbMapper;
import com.yuncheng.spcyApi.mapper.SpcyRsWjmbZjmlMapper;
import com.yuncheng.spcyApi.service.ISpcyRsWjmbZjmlService;
import com.yuncheng.spcyApi.utils.ChineseNumberConverterUtils;
import com.yuncheng.spcyApi.vo.common.PageVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import com.yuncheng.common.base.service.impl.BaseServiceImpl;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description: 文件模板-章节目录
 * @Author: yuncheng
 * @Date: 2024-04-28
 * @Version: V1.0
 */
@Service
public class SpcyRsWjmbZjmlServiceImpl extends BaseServiceImpl<SpcyRsWjmbZjmlMapper, SpcyRsWjmbZjml> implements ISpcyRsWjmbZjmlService {

    @Resource
    private SpcyRsWjmbMapper spcyRsWjmbMapper;

    @Override
    public IPage<SpcyRsWjmbZjml> queryPageList(PageVo pageVo, SpcyRsWjmbZjml query) {
        Page<SpcyRsWjmbZjml> page = new Page<>(pageVo.getPageNo(), pageVo.getPageSize());

        LambdaQueryWrapper<SpcyRsWjmbZjml> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(StringUtils.isNotBlank(query.getFwjmbid()), SpcyRsWjmbZjml::getFwjmbid, query.getFwjmbid());
        queryWrapper.eq(StringUtils.isNotBlank(query.getFsqsxid()), SpcyRsWjmbZjml::getFsqsxid, query.getFsqsxid());
        queryWrapper.like(StringUtils.isNotBlank(query.getFzjmc()), SpcyRsWjmbZjml::getFzjmc, query.getFzjmc());
        queryWrapper.orderByAsc(SpcyRsWjmbZjml::getForder);
        return page(page, queryWrapper);
    }

    @Override
    public List<SpcyRsWjmbZjml> queryList(SpcyRsWjmbZjml query) {
        LambdaQueryWrapper<SpcyRsWjmbZjml> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(StringUtils.isNotBlank(query.getFwjmbid()), SpcyRsWjmbZjml::getFwjmbid, query.getFwjmbid());
        queryWrapper.eq(StringUtils.isNotBlank(query.getFsqsxid()), SpcyRsWjmbZjml::getFsqsxid, query.getFsqsxid());
        queryWrapper.like(StringUtils.isNotBlank(query.getFzjmc()), SpcyRsWjmbZjml::getFzjmc, query.getFzjmc());
        queryWrapper.orderByAsc(SpcyRsWjmbZjml::getForder);
        return list(queryWrapper);
    }

    @Override
    public Map<String, String> getWjmbZjDescribe(String sqsxid) {
        Map<String, String> resultMap = new HashMap<>();
        String jczj = "";

        // 获取最新的事项文件模板
        List<SpcyRsWjmb> mbList = spcyRsWjmbMapper.selectList(new LambdaQueryWrapper<SpcyRsWjmb>()
                .eq(SpcyRsWjmb::getFsqsxid, sqsxid)
                .orderByDesc(SpcyRsWjmb::getCreateTime));
        if (CollectionUtil.isEmpty(mbList)){
            resultMap.put("${data.jczj}", jczj);

            return resultMap;
        }

        // 获取对应模板的章节
        List<SpcyRsWjmbZjml> zjmlList = list(new LambdaQueryWrapper<SpcyRsWjmbZjml>()
                .eq(SpcyRsWjmbZjml::getFwjmbid, mbList.get(0).getId())
                .orderByAsc(SpcyRsWjmbZjml::getForder));
        if (CollectionUtil.isEmpty(zjmlList)){
            resultMap.put("${data.jczj}", jczj);

            return resultMap;
        }

        for (SpcyRsWjmbZjml zj : zjmlList){
            jczj += "    "; // 四个空格对应文档首行缩进两位
            jczj += "（";
            jczj += ChineseNumberConverterUtils.convertToChinese(zj.getForder());
            jczj += "）";
            jczj += zj.getFzjmc();
            jczj += "\n\n"; // 换两行，相对与空一行
        }

        resultMap.put("${data.jczj}", jczj);
        return resultMap;
    }

    @Override
    public List<SpcyRsWjmbZjml> selectWjmbZjmlList(String sqsxid) {

        return spcyRsWjmbMapper.selectZjByFsqsxId(sqsxid);
        /*// 获取最新的事项文件模板
        List<SpcyRsWjmb> mbList = spcyRsWjmbMapper.selectList(new LambdaQueryWrapper<SpcyRsWjmb>()
                .eq(SpcyRsWjmb::getFsqsxid, sqsxid)
                .orderByDesc(SpcyRsWjmb::getCreateTime));
        if (CollectionUtil.isEmpty(mbList)){
            return null;
        }

        // 获取对应模板的章节
        return list(new LambdaQueryWrapper<SpcyRsWjmbZjml>()
                .eq(SpcyRsWjmbZjml::getFwjmbid, mbList.get(0).getId())
                .orderByAsc(SpcyRsWjmbZjml::getForder));*/
    }

    @Override
    public List<SpcyRsWjmbZjml> selectZjByHbsxId(String sqsxid, String fmblx3) {
        return spcyRsWjmbMapper.selectZjByHbsxId(sqsxid, fmblx3);
    }
}
